package com.zkx.fssb.controller.server;


import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.zkx.fssb.domain.dto.FlowerDto;
import com.zkx.fssb.domain.po.Flower;
import com.zkx.fssb.result.PageQuery;
import com.zkx.fssb.result.Result;
import com.zkx.fssb.service.FlowerService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;
import java.util.stream.Collectors;

@CrossOrigin
@Slf4j
@RestController("serverFlowerController")
@RequestMapping("/server/flower")
public class FlowerController {

    @Autowired
    private FlowerService flowerService;


    @GetMapping
    public Result<List<FlowerDto>> getList(){

        List<Flower> flowerList = flowerService.getBaseMapper().selectList(null);
        List<FlowerDto> flowerDtoList = flowerList.stream().map((item) -> {
            FlowerDto flowerDto = new FlowerDto();
            BeanUtils.copyProperties(item, flowerDto);
            return flowerDto;
        }).collect(Collectors.toList());
        return Result.success(flowerDtoList);


    }

    /**
     * 管理端 分页查询
     * @param pageQuery
     *
     * @return
     */
    @GetMapping("/page")
    public Result<Page> getPage(PageQuery pageQuery){
        log.info(pageQuery.toString());
        Page page =flowerService.customPage(pageQuery);



        return Result.success(page);


    }


    /**
     * 管理端 新建鲜花
     *
     * @param flowerDto
     * @return
     */
    @PostMapping
    public Result save(@RequestBody FlowerDto flowerDto){

        flowerService.customSave(flowerDto);
        return Result.success("新建鲜花成功");
    }

    /**
     * 管理端 修改鲜花
     *
     * @param flowerDto
     * @return
     */
    @PutMapping
    public Result update(@RequestBody FlowerDto flowerDto){
        flowerService.customUpdate(flowerDto);
        return Result.success("编辑鲜花成功");
    }


    /**
     * 管理端 删除鲜花
     *
     * @param flowerId
     * @return
     */
    @DeleteMapping
    public Result delete(Integer flowerId){
        boolean flag = flowerService.removeById(flowerId);
        if (flag){
            return Result.success("删除鲜花成功");
        }
        return Result.error("删除鲜花失败");
    }

    @GetMapping("/getById")
    public Result<Flower> getById(Integer flowerId){
        Flower flower = flowerService.getById(flowerId);

        return Result.success(flower);

    }

    @DeleteMapping("/batchDelete/{ids}")
    public Result batchDelete(@PathVariable List<Integer> ids){
        flowerService.batchDelete(ids);

        return Result.success("批量删除成功");

    }






}
